package com.basic.system.controller;

import com.alibaba.fastjson.JSON;
import com.basic.common.result.Errors;
import com.basic.common.result.WebResult;
import com.basic.system.dao.model.SysManagement;
import com.basic.system.domain.bo.SysMgmtBo;
import com.basic.system.service.SysMgmtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/sysMgmt")
public class SysMgmtController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private SysMgmtService sysMgmtService;

    /**
     * 查询管理制度详情数据集合List-分页
     */
    @RequestMapping(value = "/selListByParam", method = {RequestMethod.GET, RequestMethod.POST})
    public WebResult selSysManagementListByPage(@RequestBody SysMgmtBo bo) {
        logger.info("----------入参:" + JSON.toJSONString(bo));
        try {
            return WebResult.success(sysMgmtService.selListByParam(bo));
        } catch (Exception e) {
            return WebResult.fail(Errors.SYSTEM_CALL_INTERFACE_ERROR);
        }
    }

    /**
     * 根据ID查询管理制度详情信息
     */
    @RequestMapping(value = "/selById", method = {RequestMethod.GET, RequestMethod.POST})
    public WebResult selSysManagementById(@RequestBody SysMgmtBo bo) {
        try {
            return WebResult.success(sysMgmtService.selManagerAndFile(bo.getId()));

        } catch (Exception e) {
            return WebResult.fail(Errors.SYSTEM_CALL_INTERFACE_ERROR);
        }
    }

    /**
     * 添加管理制度详情信息
     */
    @PostMapping(value = "/add")
    public WebResult addSysManagement(@Validated(SysManagement.Insert.class) @RequestBody SysMgmtBo bo) {
        try {
            return WebResult.success(sysMgmtService.add(bo));

        } catch (Exception e) {
            return WebResult.fail(Errors.SYSTEM_INSERT_ERROR);
        }
    }

    /**
     * 根据ID修改管理制度详情信息
     */
    @RequestMapping(value = "/updById")
    public WebResult updSysManagementById(@RequestBody SysMgmtBo bo) {
        logger.info("----------入参:" + JSON.toJSONString(bo));
        try {
            return WebResult.success(sysMgmtService.updById(bo));

        } catch (Exception e) {
            return WebResult.fail(Errors.SYSTEM_UPDATE_ERROR);
        }
    }

    /**
     * 单点删除管理制度信息
     */
    @RequestMapping(value = "/delById", method = RequestMethod.DELETE)
    public WebResult delById(@RequestBody SysMgmtBo bo) {
        try {
            return WebResult.success(sysMgmtService.delById(bo.getId()));
        } catch (Exception e) {
            return WebResult.fail(Errors.SYSTEM_DELETE_ERROR);
        }
    }

    /**
     * 批量删除管理制度详情信息
     */
    @RequestMapping(value = "/delByIds", method = RequestMethod.DELETE)
    public WebResult delByIds(@RequestBody SysMgmtBo bo) {
        if (bo.getIds().length == 0) {
            return WebResult.fail(Errors.VALIDATION_NOT_NULL);
        }
        try {
            return WebResult.success(sysMgmtService.delByIds(bo.getIds()));

        } catch (Exception e) {
            return WebResult.fail(Errors.SYSTEM_DELETE_ERROR);
        }
    }

    /**
     * 下载
     */
    @RequestMapping("/download")
    public ResponseEntity<byte[]> DownLoad(HttpServletRequest request) {
        try {
            // DownUtils 工具类下载文件
            return sysMgmtService.download(request);
        } catch (Exception e) {
            // 处理异常
            return ResponseEntity.badRequest().body(("Failed to download file: " + e.getMessage()).getBytes());
        }
    }

    /**
     * 上传
     */
    @RequestMapping("/upload")
    public WebResult upload(@RequestParam("uploadFiles") List<MultipartFile> uploadFiles) {
        return WebResult.success(sysMgmtService.upload(uploadFiles));
    }
}


